import {login} from '../../http/user'
export let user={
    namespaced: true,
    state:{
      token:localStorage.getItem('houtaitoken'),
      get username(){
      return localStorage.getItem('houtaiusername')
      },
      set username(v){
       if(v){
         localStorage.setItem('houtaiusername',v)
       }else{
        localStorage.removeItem('houtaiusername')
       }
      }
    },       
  
    actions: {
        // 请求
        async loginFn({commit},obj){
            let res=await login(obj)
            console.log(res);
            if(res.meta.status==200){
                commit('SET_TOKEN',res.data.token)
                commit("SET_USERNAME", res.data.username)
                return Promise.resolve()
            }else{
                // 登录失败
                return Promise.reject()
            }
        }
    },
    mutations: {
        SET_TOKEN(state,token){
         state.token=token
         localStorage.setItem('houtaitoken',token)
        },
        SET_USERNAME(state, username) {
            state.username = username;
        },

        DEL_TOKEN() {
            localStorage.removeItem("houtaitoken");
        },
        DEL_USERNAME(state) {
            state.username = ""
        }
    },
}